package com.gz.web.fileupload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadHandler {

    //编写方法处理文件上传的请求

    @RequestMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file,
                             HttpServletRequest request) throws IOException {
        //接收到提交的文件名
        final String fileName = file.getOriginalFilename();
        //得到要把上传的文件保存到哪个路径
        String filePath = request.getServletContext().getRealPath("/img/" + fileName);
        //创建文件
        File saveToFile = new File(filePath);
        //将上传的文件转存到saveToFile
        file.transferTo(saveToFile);

        return "success";
    }
}
